//
//  MusicChooseViewController.m
//  享动
//
//  Created by lanou on 16/4/13.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MusicChooseViewController.h"
#import "MusicChooseCollectionViewCell.h"
#import <FMDatabase.h>
#import "musicRunModel.h"
#import "one.h"
#import "two.h"
#import "three.h"
#import "four.h"
#import "five.h"
#import "six.h"
#import "seven.h"
#import "eight.h"
#import "nine.h"
#import "MusicViewController.h"
@interface MusicChooseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic,strong)FMDatabase * db1;

@property(nonatomic,strong)two * twoView;
@property(nonatomic,strong)three * threeView;
@property(nonatomic,strong)four * fourView;
@property(nonatomic,strong)five * fiveView;
@property(nonatomic,strong)six * sixView;
@property(nonatomic,strong)seven * sevenView;
@property(nonatomic,strong)eight * eightView;
@property(nonatomic,strong)nine * nineView;
@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)NSArray * titleArray;
@property(nonatomic,strong)UIView * slidingView;

@property(nonatomic,assign)NSInteger a;
@property(nonatomic,strong)NSMutableArray * aArray;
@property(nonatomic,assign)NSInteger b;
@property(nonatomic,strong)NSMutableArray * bArray;
@property(nonatomic,assign)NSInteger c;
@property(nonatomic,strong)NSMutableArray * cArray;
@property(nonatomic,assign)NSInteger d;
@property(nonatomic,strong)NSMutableArray * dArray;
@property(nonatomic,assign)NSInteger e;
@property(nonatomic,strong)NSMutableArray * eArray;
@property(nonatomic,assign)NSInteger f;
@property(nonatomic,strong)NSMutableArray * fArray;
@property(nonatomic,assign)NSInteger g;
@property(nonatomic,strong)NSMutableArray * gArray;
@property(nonatomic,assign)NSInteger h;
@property(nonatomic,strong)NSMutableArray * hArray;
@property(nonatomic,assign)NSInteger i;
@property(nonatomic,strong)NSMutableArray * iArray;


@property(nonatomic,strong)UILabel * countLabel ;

@property(nonatomic,strong)UIButton * nextStepButton;

@property(nonatomic,strong)NSMutableArray * allArray;


@end

@implementation MusicChooseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     self.navigationItem.title = @"收藏";
    self.view.backgroundColor = [UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = rightBarButton;
    
    // 数据库
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"chooseMusic.db"];
    NSLog(@"-----------------------%@",path);
    self.db1 = [FMDatabase databaseWithPath:path];
    
    if (![self.db1 open]) {
        NSLog(@"不能打开数据库");
    }
    
    // 创建表
    
    [_db1 executeUpdate:@"CREATE TABLE chooseMusic (tableName text,albumUrl text,author text,name text,introduce text,url text,bpm integer)"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _titleArray = @[@"收藏",@"历史",@"慢跑",@"中跑",@"快跑",@"健走",@"热身",@"放松",@"街舞"];
    
    // 集合视图
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    // 布局 横向滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, KWIDTH, 32) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor darkGrayColor];
    
    // 关闭横向滚动
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.dataSource =self;
    _collectionView.delegate =self;
    
    [_collectionView registerClass:[MusicChooseCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    _slidingView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, (KWIDTH / 6), 2)];
    _slidingView.backgroundColor = [UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];

    [_collectionView addSubview:_slidingView];    [self.view addSubview:_collectionView];
    
    
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 96, KWIDTH, KHEIGHT - 96 - 49)];
    
    _scrollView.contentSize = CGSizeMake(KWIDTH * _titleArray.count, KHEIGHT-96-49);
    _scrollView.contentOffset = CGPointMake(0, 0);
    
    _scrollView.delegate = self;
    
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces= NO;
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    _scrollView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_scrollView];
    
    
    // 下面的view
    UIView * downView = [[UIView alloc]initWithFrame:CGRectMake(0, KHEIGHT - 49, KWIDTH, 49)];
    if ([_controllerStr isEqualToString:@"run"]) {
        downView.backgroundColor = [UIColor colorWithRed:128/255.0 green:197/255.0 blue:56/255.0 alpha:1];
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 49)];
        label.text = @"请选择你喜欢的音乐";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:30];
        [downView addSubview:label];
    }
    else{
        
    downView.backgroundColor = [UIColor darkGrayColor];
        _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 19/2, (KWIDTH/2)-20, 30)];
        _countLabel.textColor = [UIColor whiteColor];
        _countLabel.text = @"共选择了0首";
        _countLabel.textAlignment = NSTextAlignmentCenter;
        [downView addSubview:_countLabel];
        
        _nextStepButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _nextStepButton.frame = CGRectMake(KWIDTH /8+KWIDTH/2, 19/2, KWIDTH/4, 30);
        _nextStepButton.backgroundColor = [UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
        
        [_nextStepButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextStepButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_nextStepButton addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
        [downView addSubview:_nextStepButton];

    }
    
    [self.view addSubview:downView];
    
    
    
    
    

    
    // -----------------------------------block 传值
    __block MusicChooseViewController * blockSelf = self;
    
    _oneView = [[one alloc]init];
    _oneView.identifierOne = @"choose";
    _oneView.tableName = _tableName;
    
    _oneView.view.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT-96-49);
    _oneView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_oneView.view];
    _oneView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        
       
        _h = count;
        
        _hArray = arr;
        NSInteger h =_a + _b + _c + _d + _e  +_f + _g+ count;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",h];
        
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];
    };
    
    
    _twoView = [[two alloc]init];
    _twoView.identifierOne = @"choose";
    _twoView.tableName = _tableName;
    _twoView.view.frame = CGRectMake(KWIDTH, 0, KWIDTH , KHEIGHT-96-49);
    _twoView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_twoView.view];
    
    _twoView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        
        _i = count;
        
        _iArray = arr;
        
        NSInteger i =_a + _b + _c + _d + _e  +_f + _g+ + _h +count;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",i];
        
         blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
    };

    
    
    

    // 慢跑
    _threeView = [[three alloc]init];
    _threeView.view.backgroundColor = [UIColor whiteColor];
    _threeView.view.frame = CGRectMake(KWIDTH * 2, 0, KWIDTH, KHEIGHT-96-49);
    [_scrollView addSubview:_threeView.view];

    
    _threeView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        
        _a = count;
        
        _aArray = arr;
        NSInteger a = _b + _c + _d + _e  +_f + _g+ _h+ count+_i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",a];
        
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];
    };
    
    
    // 中跑
    _fourView = [[four alloc]init];
    
    _fourView.view.frame = CGRectMake(KWIDTH * 3, 0, KWIDTH, KHEIGHT-96-49);
    _fourView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_fourView.view];

    _fourView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _b  = count;
        _bArray = arr;
        NSInteger b = _a + _c +count + _d + _e +_f+_g +_h + _i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",b];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];


        
    };

    
    
    _fiveView = [[five alloc]init];
    _fiveView.view.frame = CGRectMake(KWIDTH * 4, 0, KWIDTH, KHEIGHT-96-49);
    _fiveView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_fiveView.view];
    _fiveView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _c = count;
        _cArray = arr;
        NSInteger c = count + _a + _b + _d + _e + _f + _g + _h + _i;
         blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",c];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
         [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];


    };

    
    _sixView = [[six alloc]init];
    _sixView.view.frame = CGRectMake(KWIDTH * 5, 0, KWIDTH, KHEIGHT-96-49);
    _sixView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_sixView.view];
    _sixView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _d = count;
        _dArray = arr;
        NSInteger d = count + _a + _b + _c + _e + _f + _g + _h + _i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",d];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
         [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];

    };
    
    _sevenView = [[seven alloc]init];
    _sevenView.view.frame = CGRectMake(KWIDTH * 6, 0, KWIDTH, KHEIGHT-96-49);
    _sevenView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_sevenView.view];
    _sevenView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _e = count;
        _eArray = arr;
        NSInteger e = count + _a + _b + _c + _d + _f + _g + _h + _i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",e];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
         [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];


    };
    
    
    _eightView = [[eight alloc]init];
    _eightView.view.frame = CGRectMake(KWIDTH * 7, 0, KWIDTH, KHEIGHT-96-49);
    _eightView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_eightView.view];
    _eightView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _f = count;
        _fArray = arr;
        NSInteger f = count + _a + _b + _c + _d + _e + _g+_h + _i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",f];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.gArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
         [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];
 
        
    };

    
    
    _nineView = [[nine alloc]init];
    _nineView.view.frame = CGRectMake(KWIDTH * 8, 0, KWIDTH, KHEIGHT-96-49);
    _nineView.view.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_nineView.view];
    _nineView.BlockC = ^(NSInteger count,NSMutableArray * arr)
    {
        _g = count;
        _gArray = arr;
        NSInteger g = count + _a + _b + _c + _d + _e + _f + _h + _i;
        blockSelf.countLabel.text = [NSString stringWithFormat:@"共选择了%ld首",g];
        blockSelf.allArray = [NSMutableArray arrayWithArray:arr];
        [blockSelf.allArray addObjectsFromArray:blockSelf.aArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.bArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.cArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.dArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.eArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.fArray];
        [blockSelf.allArray addObjectsFromArray:blockSelf.hArray];
         [blockSelf.allArray addObjectsFromArray:blockSelf.iArray];

        
    };

    _oneView.controllerStr = _controllerStr;
    _oneView.nav = self.navigationController;
    
    _twoView.controllerStr = _controllerStr;
    _twoView.nav = self.navigationController;
    
    _threeView.controllerStr = _controllerStr;
    _threeView.nav = self.navigationController;
    
    _fourView.controllerStr = _controllerStr;
    _fourView.nav = self.navigationController;
    
    
    _fiveView.controllerStr = _controllerStr;
    _fiveView.nav = self.navigationController;
    
    _sixView.controllerStr = _controllerStr;
    _sixView.nav = self.navigationController;
    
    _sevenView.controllerStr = _controllerStr;
    _sevenView.nav = self.navigationController;
    
    _eightView.controllerStr = _controllerStr;
    _eightView.nav = self.navigationController;

    _nineView.controllerStr = _controllerStr;
    _nineView.nav = self.navigationController;

    
}

- (void)nextStep

{
    // 数据库 创建上一个界面的表名的数据库
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"musicList.db"];
    
    FMDatabase * db = [FMDatabase databaseWithPath:path];
    
    [db open];
    

    
    [db executeUpdate:@"INSERT INTO musicList (listName,iconViewData,introduce) VALUES (?,?,?)",_tableNameModel.listName,_tableNameModel.iconViewData,_tableNameModel.introduce];
    
   
    

    //  插入每一个表 添加的数据
    for (int i = 0; i < _allArray.count; i ++) {
        
        musicRunModel * model = _allArray[i];
        
        
        NSNumber * a = [NSNumber numberWithInteger:model.bpm];
        
        [_db1 executeUpdate:@"INSERT INTO chooseMusic (tableName,albumUrl,author,name,introduce,url,bpm) VALUES (?,?,?,?,?,?,?)",_tableName,model.albumUrl,model.author,model.name,model.introduce,model.url,a];
      

        
    }
    
    
    
    
    
    // 添加好了 返回上一个界面
   
        dispatch_async(dispatch_get_main_queue(), ^{
            MusicViewController * music = [[MusicViewController shareInstence]initWithNibName:@"musicRun" bundle:[NSBundle mainBundle]];
        // 重新取插入的数据
        music.musicListArray = [NSMutableArray array];
        
         
            
        FMResultSet * rs = [db executeQuery:@"SELECT * FROM musicList"];
        
        while ([rs next]) {
            
            musicListModel * model = [[musicListModel alloc]init];
            model.listName = [rs stringForColumn:@"listName"];
            model.iconViewData = [rs dataForColumn:@"iconViewData"];
            model.introduce = [rs stringForColumn:@"introduce"];
            [music.musicListArray addObject:model];
        }
            
        [rs close];
            
            
        // 更新界面
            music.nameT.text = nil;
            music.nameT1.text = nil;
            music.iconView.image  = nil;
            
       
        music.addView.frame =  CGRectMake((KWIDTH-10)/2, KHEIGHT - 10, 0 , 0);
 
        [music.conllectionView reloadData];
          
            
           
        });
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    

    
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        
        NSInteger row = scrollView.contentOffset.x / KWIDTH;
        self.navigationItem.title = _titleArray[row];
        
        [UIView animateWithDuration:0.3 animations:^{
             _slidingView.frame = CGRectMake(row * (KWIDTH / 6), 30, KWIDTH / 6, 2);
        }];
        
        
        
        if (row >= 4 && row <= 6) {
            [_collectionView setContentOffset:CGPointMake(KWIDTH / 6 * (row - 3), 0) animated:YES];
        } else if (row > 6) {
            [_collectionView setContentOffset:CGPointMake(KWIDTH / 6 * 3, 0) animated:YES];
        } else {
            [_collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
        
       
    }
    
   
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MusicChooseCollectionViewCell * cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.smallLabel.text = _titleArray[indexPath.row];
    cell.smallLabel.textColor = [UIColor whiteColor];
    
    
    return cell;
}

#pragma mark --- 控制集合视图的行边距 ---
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KWIDTH / 6, 30);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if (indexPath.row >= 4 && indexPath.row <= 6) {
        [_collectionView setContentOffset:CGPointMake(KWIDTH / 6 * (indexPath.row - 3), 0) animated:YES];
    } else if (indexPath.row >= 7) {
        [_collectionView setContentOffset:CGPointMake(KWIDTH / 6 * 3, 0) animated:YES];
    } else if (indexPath.row < 4) {
        [_collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    
    
    [UIView animateWithDuration:0.8 animations:^{
         _slidingView.frame = CGRectMake(indexPath.row * (KWIDTH / 6), 30, (KWIDTH / 6), 2);
    }];
    _scrollView.contentOffset = CGPointMake(indexPath.row * KWIDTH, 0);
    

    self.navigationItem.title = _titleArray[indexPath.row];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;

}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)rightBarButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
